package LeetCode.leetcode;

/**
 * 加一
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/24
 * @time: 09:31
 */
public class Test66加一 {
    public static void main(String[] args) {
        int[] digits = {9,9,9,9};
        int[] result = plusOne(digits);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }

    }

    public static int[] plusOne(int[] digits) {
        if (digits == null || digits.length == 0) {
            return digits;
        }
        int takeOver = 0;
        for (int i = digits.length - 1; i >= 0; i--) {
             int sum = digits[i]  + takeOver;
             if (i == digits.length - 1) {
                 sum += 1;
             }
             if (sum >= 10) {
                 sum -= 10;
                 takeOver = 1;
             } else {
                 takeOver = 0;
             }
             digits[i] = sum;
        }
        if (takeOver == 1) {
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            int j = 0;
            for (int i = 1; i < result.length; i++) {
                result[i] = digits[j];
                j++;
            }
            return result;
        } else {
            return digits;
        }

    }
}
